<template>
  <el-form ref="form" :model="form" :rules="rules" class="login-container" label-width="80px" v-loading='loading'>
    <h3 class="login_title">系统登录</h3>
    <el-form-item label="用户名" required prop="username">
        <el-input v-model="form.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" required prop="password">
        <el-input v-model="form.password" show-password></el-input>
    </el-form-item>
    <el-form-item label-width="0">
        <el-checkbox  v-model="form.checked" label-position="left">记住密码</el-checkbox>
    </el-form-item>
    <el-form-item label-width="0">
        <el-button type="primary" @click="onSubmit('form')">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { postRequest }  from '@/utils/http';
export default {
    data() {
      return {
        form: {
          username: '',
          password: '',
          checked: true
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ]
        },
        loading: false
      }
    },
    methods: {
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.loading = true;
            postRequest('/api/login',{
              username: this.form.username,
              password: this.form.password
            })
            .then(resp => {
              this.loading = false;
              if(resp.data.status == 'success'){
                this.$router.replace({path: '/home',query:{username:this.form.username}});
              } else {
                this.$alert(resp.data.msg, '提示', {
                  confirmButtonText: '确定'
                });
              }
            },err => {
              this.loading = false;
              this.$alert('请联系管理员', '错误', {
                  confirmButtonText: '确定'
              });
              console.log('错误',err);
            });
          } else {
            this.$alert('请联系管理员', '错误', {
                confirmButtonText: '确定'
            });
            return false;
          }
        });
      }
    }
}
</script>

<style>
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
</style>